<?php
namespace Home\Model;
use Think\Model;
class ProcessModel extends Model {
	public $table='zjy_process';
	public $page; //当前页
	public $perpage; //每页显示
	public $process_id; //ID
	public $pc_id; //分类ID
	public $post; //提交的信息 数组
	public $ids; //IDs 主要用于删除
	public $sorts; //IDs 主要用于删除
	public $keyword; //关键词
	public $pc_ids; //获取分类ID下的子分类
	public $process_sort;      //排序
	public $member_id;
	public $page_type;     //1会员中心2现货首页
	//提交
	public function process_add_post()
	{
		$Model=M("Process");
		$data=$this->post;
		if($Model->add($data)){
			return true;
		}else{
			return false;
		}
	}
	public function get_process_list()
	{
		$page=$this->page;
		$perpage=$this->perpage;
		//搜索
		$where='where 1 ';
		$order='process_sort asc,process_id desc';
		/*if($this->keyword){
			$where.="and `process_title` LIKE '%".$this->keyword."%'";
		}*/
		if($this->page_type==2){
			$where.='and member_id='.$this->member_id.' ';
		}
		//排序
		if($this->process_sort){
			$order=$this->process_sort;
		}
		/*
		if($this->pc_id){
			$pc_ids=$this->get_sub_pc_id($this->pc_id);
			$pc_ids=mb_strcut($pc_ids,1); //裁掉第一个，
			$where.="and c.pc_id in(".$pc_ids.")";
		}*/
		$start=($page-1)*$perpage;
		$Model=new Model();
		$Member=M("Member");
		$ProductClass=M("ProductClass");
		$Brand=M("Brand");
		$Spec=M("Spec");
		$Unit=M("Unit");
		$Depot=M("Depot");
		$Area=M("Area");
		$DeliverTime=M("DeliverTime");
		$DeliverType=M("DeliverType");
		$ShippingType=M("ShippingType");
		$sql="select * from ".$this->table." ".$where." ORDER BY ".$order." limit $start,$perpage";
		$list=$Model->query($sql);
		//print_r($list);exit;
		foreach($list as $k=>$v){
			$list[$k]['member_truename']=$Member->where('member_id='.intval($v['member_id']))->getField('member_truename');
			$list[$k]['member_company']=$Member->where('member_id='.intval($v['member_id']))->getField('member_company');
			$list[$k]['member_area_id']=$Member->where('member_id='.intval($v['member_id']))->getField('member_area_id');
			$list[$k]['member_area_name']=$Area->where('area_id='.intval($list[$k]['member_area_id']))->getField('area_name');
			$list[$k]['pc_name']=$ProductClass->where('pc_id='.intval($v['pc_id']))->getField('pc_name');
			$list[$k]['brand_name']=$Brand->where('brand_id='.intval($v['brand_id']))->getField('brand_name');
			$list[$k]['spec_name']=$Spec->where('spec_id='.intval($v['spec_id']))->getField('spec_name');
			$list[$k]['unit_name']=$Unit->where('unit_id='.intval($v['unit_id']))->getField('unit_name');
			$list[$k]['depot_area_id']=$Depot->where('depot_id='.intval($v['depot_id']))->getField('depot_area_id');
			$list[$k]['depot_area_name']=$Area->where('area_id='.intval($list[$k]['depot_area_id']))->getField('area_name');
			$list[$k]['depot_name']=$Depot->where('depot_id='.intval($v['depot_id']))->getField('depot_name');
			$list[$k]['deliver_time_title']=$DeliverTime->where('dt_id='.intval($v['deliver_time_id']))->getField('dt_title');
			$list[$k]['deliver_type_title']=$DeliverType->where('dt_id='.intval($v['deliver_type_id']))->getField('dt_title');
			$list[$k]['shipping_type_title']=$ShippingType->where('st_id='.intval($v['shipping_type_id']))->getField('st_title');
			$list[$k]['time_to_word']=$this->time_to_str(intval($v['process_time']));
//			$list[$k]['process_state_name']=$processState->where('process_state_id='.intval($v['process_state_id']))->getField('process_state_name');
		}
		//print_r($list);exit;
		return $list;
	}
	private function time_to_str($time) {
		$time = (int) substr($time, 0, 10);
		$int = time() - $time;
		$str = '';
		if ($int <= 2){
			$str = sprintf('刚刚', $int);
		}elseif ($int < 60){
			$str = sprintf('%d秒前', $int);
		}elseif ($int < 3600){
			$str = sprintf('%d分钟前', floor($int / 60));
		}elseif ($int < 86400){
			$str = sprintf('%d小时前', floor($int / 3600));
		}elseif ($int < 2592000){
			$str = sprintf('%d天前', floor($int / 86400));
		}else{
			$str = date('Y-m-d H:i:s', $time);
		}
		return $str;
	}
	//获得总数
	public function get_process_total()
	{
		//搜索
		$where='where 1 ';
		if($this->keyword){
			$where.="and `process_title` LIKE '%".$this->keyword."%'";
		}
		if($this->page_type==2){
			$where.='and member_id='.$this->member_id.' ';
		}
		/*if($this->pc_id){
			$pc_ids=$this->get_sub_pc_id($this->pc_id);
			$pc_ids=mb_strcut($pc_ids,1); //裁掉第一个，
			$where.="and pc_id in(".$pc_ids.")";
		}*/
		$Model=new Model();
		$sql="select count(process_id) as total from ".$this->table." ".$where;
		$query=$Model->query($sql);
		return $query[0]['total'];
	}
	//AJAX获取弹窗需要的的数据
	public function get_ajax_info()
	{
		$process_id=intval($this->process_id);
		$Model=M("Process");
		$info=$Model->where('process_id='.$process_id)->find();
		$Member=M('Member');
		$info['member_truename']=$Member->where('member_id='.$info['member_id'])->getField('member_truename');
		return $info;
	}
	//编辑提交
	public function process_edit_post()
	{
		$Model=M("Process");
		$condition['process_id'] = $this->post['process_id'];
		$data=$this->post;
		//print_r($this->post);exit;
		$result=$Model->where($condition)->save($data);
		if($result === false) {
			return false;
		}else{
			return true;
		}
	}
	//AJAX
	public function get_process_info()
	{
		$process_id=intval($this->process_id);
		$Model=M("Process");
		$info=$Model->where('process_id='.$process_id)->find();
		return $info;
	}

	public function process_delete()
	{
		$ids=$this->ids;
		$Model=M("Process");
		foreach($ids as $v){
			$condition['process_id']=$v;
			if(!$Model->where($condition)->delete()){
				return false;
			}
		}
		return true;
	}

}